import { useReactive } from "ahooks";
import { useModel } from "umi";
import { useEffect } from "react";
import compress from "gql-compress";

const useAddStaff = () => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
    });

    const addStaff = async (values, callback) => {
        state.loading = true;
        let variables = {
            osUuid: uuidv4(),
            osName: values.omName,
            omUuid: values.omUuid,
            osBirt: values.omBirt,
            osFrom: values.osFrom,
            osIdnu: values.omIdnu,
            osNati: values.omNati,
            osPhon: values.omPhon,
            osRoge: values.omRoge,
            osStat: values.osStat,
            osType: values.osType,
        };
        let res = await graphqlFetch({
            query,
            variables,
        });
        res && callback && callback(res);
        state.loading = false;
    };

    return { ...state, addStaff };
};

let query = compress(`mutation MyMutation(
    $osUuid: UUID!
    $osName: String!
    $omUuid: UUID!
    $osBirt: Date
    $osFrom: String!
    $osIdnu: String
    $osNati: String
    $osPhon: String
    $osRoge: String
    $osStat: Boolean!
    $osType: String!
) {
    createOrganizationStaff(
        input: {
            organizationStaff: {
                osName: $osName
                osUuid: $osUuid
                omUuid: $omUuid
                osBirt: $osBirt
                osFrom: $osFrom
                osIdnu: $osIdnu
                osNati: $osNati
                osPhon: $osPhon
                osRoge: $osRoge
                osStat: $osStat
                osType: $osType
            }
        }
    ) {
        clientMutationId
    }

    updateOrganizationMemberByOmUuid(input: { organizationMemberPatch: { omIfim: true }, omUuid: $omUuid }) {
        clientMutationId
    }
}`);

export default useAddStaff;
